var addLinkImage = (function (window, $, document, undefined) {
    var basePath = $('#basePath').val();

    /*
     *为"目标商品选择"按钮的点击设置事件
     * */
    function targetCommodityChooseButton() {
        $("#targetCommodityChoose").click(function () {
            $.ajax({
                type: 'POST',
                url: basePath + 'commodity/retrieveAllValidCommodities.do',
                dataType: 'json',
                beforeSend: function () {
                    $("#loadingDiv").show();
                },
                success: function (data) {
                    //noinspection JSUnresolvedVariable
                    generateCommodityTypeList(data.allCommodityType);
                    //noinspection JSUnresolvedVariable
                    generateCommodityList(data.allCommodity);
                    $("#loadingDiv").hide();
                    $("#targetCommodityList").modal("show");
                }
            });
        });
    }

    /*
     * This method is used to generate commodity type dropList.
     * */
    function generateCommodityTypeList(allCommodityType) {
        var typeLength = allCommodityType.length, i = 0, oneOption, $commodityTypeList = $("#commodityTypeList");
        $commodityTypeList.html("");
        for (; i < typeLength; i++) {
            oneOption = '<option value="' + allCommodityType[i].id + '">' + allCommodityType[i].typeName + '</option>';
            $commodityTypeList.append(oneOption);
        }
    }

    function generateCommodityList(allCommodity) {
        var $commodityListContainer = $("#commodityListContainer > div"), allCommodityLength = allCommodity.length,
            result = "", i = 0;
        $commodityListContainer.html("");
        for (; i < allCommodityLength; i++) {
            if (i % 4 === 0) {
                result += '<div class="customRow">';
            }

            result += createOneCommodity(allCommodity[i]);

            if ((i + 1) % 4 === 0) {
                result += "</div>";
            }
        }

        if (allCommodityLength % 4 !== 0) {
            result += "</div>";
        }

        $commodityListContainer.append(result);
    }

    function chooseOneCommodityButtonClick() {
        $("#commodityListContainer").on("click", "a.chooseCommodity", function () {
            var $this = $(this), $targetInfoSpan = $this.parent().find("span"),
                $targetCommodityResult = $("#targetCommodityResult"), resultStr = "";
            $("#targetCommodityList").modal("hide");
            resultStr += '<div class="cityLabel">' + $targetInfoSpan.attr("cityLabel") + '</div>';
            resultStr += '<img src="imagePhoto/firstImage.do?commodityId=' + $targetInfoSpan.attr("commodityId") + '"/>';
            resultStr += '<div class="commodityName">' + $targetInfoSpan.attr("commodityName") + '</div>';
            resultStr += '<input type="hidden" name="commodityId" value="' + $targetInfoSpan.attr("commodityId") + '"/>';
            $targetCommodityResult.html(resultStr);
        });
    }

    /*这个方法用于创建一个商品拼接后的字符串*/
    function createOneCommodity(oneCommodity) {
        var result = "";
        result += '<div class="commodityInfo">';
        result += '<div class="cityLabel">' + oneCommodity.provinceInfo.provinceName + '</div>';
        result += '<img src="imagePhoto/firstImage.do?commodityId=' + oneCommodity.id + '"/>';
        result += '<div class="commodityName">' + oneCommodity.baseInfoDescription + '</div>';
        result += '<div>';
        result += '<a type="button" class="btn btn-info chooseCommodity">选择</a>';
        result += '<a type="button" class="btn btn-info pull-right">查看详情</a>';
        result += '<span commodityId="' + oneCommodity.id + '" cityLabel="' + oneCommodity.provinceInfo.provinceName + '" commodityName="' + oneCommodity.baseInfoDescription + '"style="display:none"></span>'
        result += '</div>';
        result += '</div>';

        return result;
    }

    /*
     * This method is used to add click event for abandonAndReturn button, and will skip to
     * link's image manage interface.
     * */
    function abandonAndReturnButton() {
        $("#abandonAndReturn").click(function (event) {
            event.preventDefault();
            window.location.href = basePath + "linkImageSkip/linkImageManage.do";
            return false;
        });
    }

    return {
        init: function () {
            targetCommodityChooseButton();
            abandonAndReturnButton();
            chooseOneCommodityButtonClick();
        }
    };
})(window, jQuery, document);